angular.module('msgboxcontrollers',[])
  //发现的首页（约局）的Controller
  .controller('MsgBoxCtrl',function($scope,$state,$stateParams,Loading,MsgBox,Storage,History,Toast){
    var memberId = '';
    $scope.msgBoxList = [];
    var readIndex = '';
    $scope.$on('$ionicView.beforeEnter',function(){
      memberId = Storage.get("member").member_id;
      MsgBox.getMsgBoxList(memberId);
    });
    $scope.$on('MsgBox.msgBoxListUpdated',function(){
      $scope.msgBoxList = MsgBox.getMsgBoxListJson().msgBoxList;
    });
    //点击则将消息盒子标记为已读
    $scope.readMsgBox = function(mnId,index){
      if($scope.msgBoxList[index].mn_state==0){
        readIndex = index;
        MsgBox.updateMsgState(mnId);
      }
    }
    $scope.$on('MsgBox.updateMsgStateUpdated',function(){
      var result = MsgBox.getUpdateStateJson();
      if(result.updateMsgState){
        if($scope.msgBoxList[readIndex].mn_state==0){
          Toast.showMessage("标记为已读");
          $scope.msgBoxList[readIndex].mn_state=1;
        }
        readIndex = '';
      }else{
        Toast.showMessage("网络异常，请稍后再试");
      }
    });

    /****************删除消息盒子*******************/
    $scope.selectMsgBoxCount = 0;
    $scope.allSelected = false;
    $scope.rightBtnText = '编辑';
    $scope.showEdit = false;
    $scope.clickEditBtn = function(){
      if($scope.rightBtnText=='编辑'){
        $scope.rightBtnText='取消';
        $scope.showEdit = true;
      }else if($scope.rightBtnText=='取消'){
        $scope.rightBtnText='编辑';
        $scope.showEdit = false;
        //点击取消的时候，清空所有的记录
        $scope.selectMsgBoxCount = 0;
        $scope.allSelected = false;
        for(i=0;i<$scope.msgBoxList.length;i++){
          $scope.msgBoxList[i].selected = false;
        }
      }
    }
    //选择消息盒子
    $scope.selectMsgBox = function(index){
      if($scope.msgBoxList[index].selected){
        $scope.selectMsgBoxCount--;
        $scope.allSelected = false;
      }else{
        $scope.selectMsgBoxCount++;
      }
      $scope.msgBoxList[index].selected = !$scope.msgBoxList[index].selected;
    }
    //全选消息盒子
    $scope.selectAllMsgBox = function(){
      if($scope.allSelected){
        $scope.selectMsgBoxCount=0;
        for(i=0;i<$scope.msgBoxList.length;i++){
          $scope.msgBoxList[i].selected = false;
        }
      }else{
        $scope.selectMsgBoxCount=$scope.msgBoxList.length;
        for(i=0;i<$scope.msgBoxList.length;i++){
          $scope.msgBoxList[i].selected = true;
        }
      }
      $scope.allSelected = !$scope.allSelected;
    }
    //删除消息盒子
    $scope.delMsgBoxList = function(){
      var deleteMsgBoxIds = [];
      for(i=0;i<$scope.msgBoxList.length;i++){
        if($scope.msgBoxList[i].selected){
          deleteMsgBoxIds.push($scope.msgBoxList[i].mn_id);
        }
      }
      if(deleteMsgBoxIds.length>0){
        Loading.showLoading();
        MsgBox.deleteMsgBox(deleteMsgBoxIds);
      }else{
        Toast.showMessage("请选择要操作的消息");
      }
    }
    $scope.$on('MsgBox.deleteMsgBoxUpdated',function(){
      Loading.hideLoading();
      var result = MsgBox.getDeleteMsgBoxJson();
      if(result.state){
        MsgBox.getMsgBoxList(memberId);
        $scope.selectMsgBoxCount = 0;
        $scope.allSelected = false;
      }
      Toast.showMessage(result.message);
    });

    //将消息盒子标记为已读
    $scope.readMsgBoxList = function(){
      var deleteMsgBoxIds = [];
      for(i=0;i<$scope.msgBoxList.length;i++){
        if($scope.msgBoxList[i].selected){
          deleteMsgBoxIds.push($scope.msgBoxList[i].mn_id);
        }
      }
      if(deleteMsgBoxIds.length>0){
        Loading.showLoading();
        MsgBox.readMsgBox(deleteMsgBoxIds);
      }else{
        Toast.showMessage("请选择要操作的消息");
      }
    }

    $scope.$on('MsgBox.readMsgBoxUpdated',function(){
      memberId = Storage.get("member").member_id;
      MsgBox.getMsgBoxList(memberId);
    });

    $scope.goback = function(){
      History.goback();
    }
  });
